package com.jinlejia.feedback.question.adapter.question;


import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.freedom.yefeng.yfrecyclerview.YfMultiTypeListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbFooterLoadingBinding;

import java.util.ArrayList;
import java.util.Map;

public class QuestionDetailsAdapter extends YfMultiTypeListAdapter {
    private static final int TYPE_TITLE = 10; //标题
    private static final int TYPE_SOLUTION = 11;//解决方案
    private static final int TYPE_LATEST_PROGRESS = 12; //最新进度
    private static final int TYPE_REPLY = 13;   //问题回复
    private static final int TYPE_LATEST_TITLE_PROGRESS = 14; //最新进度标题
    private ArrayList<Map<String, Object>> dataList;

    public QuestionDetailsAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
        dataList = data;
    }

    public void setDataList(ArrayList<Map<String, Object>> data) {
        super.setData(data, YfListMode.MODE_DATA);
        this.dataList = data;
        notifyDataSetChanged();
    }

    public void addDataList(ArrayList<Map<String, Object>> data) {
        super.addData(data);
        this.dataList.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public void onBindCustomDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        //empty
    }

    @Override
    public RecyclerView.ViewHolder onCreateCustomDataViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE_TITLE: //标题
                holder = new TitleViewHolderHelper.ViewHolder(inflater.inflate(R.layout.fb_adapter_question_details_title, parent, false));
                break;
            case TYPE_LATEST_PROGRESS: //最新进度
                holder = new LatestProgressHolderHelper.ViewHolder(
                        inflater.inflate(R.layout.fb_adapter_question_details_progress, parent, false));
                break;
            case TYPE_LATEST_TITLE_PROGRESS: //最新进度标题
                holder = new LatestProgressTitleHolderHelper.ViewHolder(
                        inflater.inflate(R.layout.fb_adapter_question_details_progress, parent, false));
                break;
            case TYPE_REPLY:
                holder = new QuestionListHolderHelper.ViewHolder(
                        inflater.inflate(R.layout.fb_adapter_question_details_list, parent, false));
                break;
            case TYPE_SOLUTION:
                holder = new SolutionHolderHelper.ViewHolder(inflater.inflate(R.layout.fb_adapter_question_solution, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (dataList == null || dataList.size() <= position) {
            return;
        }
        if (holder instanceof TitleViewHolderHelper.ViewHolder) {
            TitleViewHolderHelper.bindViewHolder(eventListener,this, (TitleViewHolderHelper.ViewHolder) holder, dataList, position);
        } else if (holder instanceof LatestProgressHolderHelper.ViewHolder) {
            LatestProgressHolderHelper.bindViewHolder(eventListener,this, (LatestProgressHolderHelper.ViewHolder) holder, dataList, position);
        } else if (holder instanceof LatestProgressTitleHolderHelper.ViewHolder) {
            LatestProgressTitleHolderHelper.bindViewHolder(eventListener,this, (LatestProgressTitleHolderHelper.ViewHolder) holder, dataList, position);
        } else if (holder instanceof QuestionListHolderHelper.ViewHolder){
            QuestionListHolderHelper.bindViewHolder(eventListener,this, (QuestionListHolderHelper.ViewHolder) holder, dataList, position);
        }else if (holder instanceof SolutionHolderHelper.ViewHolder){
            SolutionHolderHelper.bindViewHolder(eventListener,this, (SolutionHolderHelper.ViewHolder) holder, dataList, position);
        }
    }


    private KeyHelper.EventListener eventListener;

    public void setEventListener(KeyHelper.EventListener eventListener) {
        this.eventListener = eventListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }


    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_footer_loading, parent, false);
        return new FootViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FootViewHolder){
            FbFooterLoadingBinding binding = DataBindingUtil.getBinding(holder.itemView);
            String message = String.valueOf(mFooters.get(0));
            if ("没有更多了".equals(message)){
                binding.txt.setText(message);
                binding.txt.setVisibility(View.VISIBLE);
                binding.progress.setVisibility(View.GONE);
            }else {
                binding.txt.setVisibility(View.GONE);
                binding.progress.setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (dataList == null || dataList.size() == 0) {
            return super.getItemViewType(position);
        }
        if (position>=dataList.size()){
            return super.getItemViewType(position);
        }
        Map<String, Object> map = dataList.get(position);
        String type = String.valueOf(map.get("type"));
        if ("1".equals(type)) { //标题
            return TYPE_TITLE;
        } else if ("2".equals(type)) { //解决方案
            return TYPE_SOLUTION;
        } else if ("3".equals(type)) { //最新进度标题
            return TYPE_LATEST_TITLE_PROGRESS;
        } else if ("4".equals(type)) { //最新进度
            return TYPE_LATEST_PROGRESS;
        } else if ("5".equals(type)) { //问题回复
            return TYPE_REPLY;
        } else {
            return super.getItemViewType(position);
        }
    }

    @Override
    public int getItemCount() {
//        if (dataList == null || dataList.size() == 0) return 1;
        return super.getItemCount();
    }

}
